package com.example.dianproject.controller;

import com.example.dianproject.dto.LoginRequest;
import com.example.dianproject.dto.LoginResponse;
import com.example.dianproject.dto.RegisterRequest;
import com.example.dianproject.dto.RegisterResponse;
import com.example.dianproject.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
    
    private final UserService userService;
    
    @PostMapping("/login")
    public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
        log.info("收到登录请求: {}", request);
        try {
            LoginResponse response = userService.login(request);
            log.info("登录响应: {}", response);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("登录失败", e);
            return ResponseEntity.ok(LoginResponse.error("登录失败：" + e.getMessage()));
        }
    }

    @PostMapping("/register")
    public ResponseEntity<RegisterResponse> register(@RequestBody RegisterRequest request) {
        log.info("收到注册请求: {}", request);
        try {
            RegisterResponse response = userService.register(request);
            log.info("注册响应: {}", response);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("注册失败", e);
            return ResponseEntity.ok(RegisterResponse.error("注册失败：" + e.getMessage()));
        }
    }
} 